package com.yztc.love_travel.ui.mainservice.buy.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.yztc.love_travel.R;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BuyDetailActivity extends AppCompatActivity {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.activity_buy_detail)
    RelativeLayout activityBuyDetail;

    private int articleId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_detail);
        ButterKnife.bind(this);

        Intent intent=getIntent();
        articleId = intent.getIntExtra(OtherConstant.KEY_BUYING_ID, 0);//获取Id
        initWebView();
    }

    //释放WebView资源
    @Override
    protected void onDestroy() {
        super.onDestroy();

        webView.stopLoading(); //停止加载
        ((ViewGroup)webView.getParent()).removeView(webView); //把webview从视图中移除
        webView.removeAllViews(); //移除webview上子view
        webView.clearCache(true); //清除缓存
        webView.clearHistory(); //清除历史
        webView.destroy(); //销毁webview自身

    }

    //初始化WebView
    private void initWebView() {
        String detailsUrl = URLConstant.Buying.URL_BUYING_DETAILS +  articleId;
        webView.loadUrl(detailsUrl);
        //设置支持js
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            //页面加载开始
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
            //页面加载结束
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
    }
}
